package com.bluetooth.phone.multi;

import android.graphics.Rect;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.recyclerview.widget.StaggeredGridLayoutManager;

public class DivideItemDecoration extends RecyclerView.ItemDecoration {
    // item的水平、垂直方向的间距
    private int verticalSpacing = 0;
    private int horizontalSpacing = 0;

    // recyclerview 左右分别额外的边距，可正可负
    private int extraSpacing = 0;

    // item占满一行时，该item是否需要左右间距
   public boolean needVerticalSpacingInSingleLine = true;
    public boolean needHorizontalSpacingInSingleLine = true;


    public DivideItemDecoration(int spacing) {
        initSpacing(spacing, spacing, 0);
    }

    public DivideItemDecoration(int spacing, int extraSpacing) {
        initSpacing(spacing, spacing, extraSpacing);
    }

    public DivideItemDecoration(int verticalSpacing, int horizontalSpacing, int extraSpacing) {
        initSpacing(verticalSpacing, horizontalSpacing, extraSpacing);
    }

    private void initSpacing(int verticalSpacing, int horizontalSpacing, int extraSpacing) {
        this.horizontalSpacing = horizontalSpacing;
        this.verticalSpacing = verticalSpacing;
        this.extraSpacing = 0;
    }

    /**
     * 1:算出均等间距的公式
     *
     * 假设
     * 每行总item数：n
     * 均等间距：spacing
     * 每个item所在行的下标：index
     * 每个item占用的总间距：itemAllSpacing = spacing *（n + 1）/n
     *
     * index = 0
     *     left = spacing  -> spacing
     *     right = itemAllSpacing - spacing
     *
     * index = 1
     *      left = spacing - (itemAllSpacing - spacing)   ->   2 * spacing - itemAllSpacing
     *      right = itemAllSpacing - (2 * spacing - itemAllSpacing)   ->   2 * itemAllSpacing - 2 * spacing
     *
     * index = 2
     *      left = spacing - (2*itemAllSpacing - 2 * spacing)   ->   3 * spacing - 2 * itemAllSpacing
     *
     * 由上可得出结论：
     * 每个item的left = (index + 1) * spacing - index * itemAllSpacing
     * 每个item的right = itemAllSpacing - 每个item的left
     *
     * 2:针对整个recyclerview左右的间距可能大于或小于中间间距的特殊情况
     * 增加一个recyclerview左右2个额外间距的字段：extraSpacing
     * 则上面的itemAllSpacing = (spacing * (n + 1) + extraSpacing * 2) / n
     *
     * 3.针对占满一行的item的特殊情况（如banner、推荐行、广告等）
     * 针对这种情况，可能需要左右间距，也可能不需要左右间距
     * 新增字段是否需要左右间距：needVerticalSpacingInSingleLine
     *
     * left = if(needVerticalSpacingInSingleLine) spacing + extraSpacing else 0
     * right = if(needVerticalSpacingInSingleLine) spacing + extraSpacing else 0
     * @param outRect
     * @param view
     * @param parent
     * @param state
     */
    @Override
    public void getItemOffsets(@NonNull Rect outRect, @NonNull final View view, @NonNull RecyclerView parent,
                               @NonNull RecyclerView.State state) {
        int position = parent.getChildAdapterPosition(view);
        int spanCount = 0;
        int spanSize = 0;
        int spanIndex = 0;
        RecyclerView.LayoutManager layoutManager = parent.getLayoutManager();
        ViewGroup.LayoutParams viewLayoutParams = view.getLayoutParams();
        if (layoutManager instanceof StaggeredGridLayoutManager) {
            StaggeredGridLayoutManager staggeredGridLayoutManager = (StaggeredGridLayoutManager) layoutManager;
            spanCount = staggeredGridLayoutManager.getSpanCount();
            if (viewLayoutParams instanceof StaggeredGridLayoutManager.LayoutParams) {
                StaggeredGridLayoutManager.LayoutParams params = (StaggeredGridLayoutManager.LayoutParams) viewLayoutParams;
                if (params.isFullSpan()) {
                    spanIndex = params.getSpanIndex();
                }
            }
        } else if (layoutManager instanceof GridLayoutManager) {
             final GridLayoutManager gridLayoutManager = (GridLayoutManager) layoutManager;
            spanCount = gridLayoutManager.getSpanCount();
            spanSize = gridLayoutManager.getSpanSizeLookup().getSpanSize(position);
            if (viewLayoutParams instanceof GridLayoutManager.LayoutParams) {
                GridLayoutManager.LayoutParams params = (GridLayoutManager.LayoutParams) viewLayoutParams;
                spanIndex = params.getSpanIndex();
            }
        } else if (layoutManager instanceof LinearLayoutManager) {
            outRect.left = verticalSpacing;
            outRect.right = verticalSpacing;
            outRect.bottom = horizontalSpacing;
            outRect.top =  10;
        }

        if (spanSize == spanCount) {
            outRect.left = needVerticalSpacingInSingleLine ? verticalSpacing + extraSpacing : 0;
            outRect.right = needVerticalSpacingInSingleLine ? verticalSpacing + extraSpacing : 0;
            outRect.bottom = needHorizontalSpacingInSingleLine ? horizontalSpacing : 0;
            outRect.top =  10;
        } else {
            int itemAllSpacing = (verticalSpacing * (spanCount + 1) + extraSpacing * 2) / spanCount;
            int left = verticalSpacing * (spanIndex + 1) - itemAllSpacing * spanIndex + extraSpacing;
            int right = itemAllSpacing - left;
            outRect.left = left;
            outRect.right = right;
            outRect.bottom = horizontalSpacing;
            outRect.top =  10;
        }

    }
}
